package com.swu.pad.util.ExcelUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.web.multipart.MultipartFile;

public class ExcelReader {

	/**
	 * 替换Excel中的表达式
	 */
	public static List<Map<String, String>> readRromExcel(MultipartFile file) {
		try {
			HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
			HSSFSheet sheet = workbook.getSheetAt(0);
			DataFormatter df = new DataFormatter();
			int firstRowNum = sheet.getFirstRowNum();
			Row firstRow = sheet.getRow(firstRowNum);
			List<Map<String, String>> list = new ArrayList<Map<String, String>>();
			for (Row row : sheet) {
				if (row.getRowNum() == firstRowNum)
					continue;
				Map<String, String> map = new HashMap<String, String>();
				for (Cell cell : row) {
					String cellValue = df.formatCellValue(cell).trim();
					map.put(firstRow.getCell(cell.getColumnIndex()).getStringCellValue().trim(), cellValue);
				}
				list.add(map);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
